<script>
export default {
  name: "Login",
  data(){
    return {
      //表单数据对象:model
      loginData:{
        userAccount:null,//属性v-model
        userPassword:null
      },
      //验证
      loginRules:{
        userAccount: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    login(){
      //全局验证
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          //验证成功，登录
          this.$http({url:"/user/login",method:"POST",data:this.loginData}).then(res=>{
           if(res.data.code==200){
             //登录成功
             this.$message({
               message: res.data.message,
               type: 'success'
             });
             //保存token
             window.sessionStorage.setItem("token",res.data.data)
             //重置表单
             this.$refs.loginForm.resetFields();
             //跳转路由
             this.$router.push("/home")
           }else{
             //登录失败
             this.$message.error(res.data.message);
             //重置表单
             this.$refs.loginForm.resetFields();
           }
          })
        } else {
          this.$message.error('请输入正确的登录信息');
          return false;
        }
      });
      //发送请求

    },
    register(){
      this.$router.push("/register")
    }
  }
}
</script>

<template>
  <div id="login">

    <div id="login_div">
      <h1>登录</h1>
      <!--element-ui表单
         ref="loginForm":给当前表单对象取名loginForm
         model:绑定表单数据对象,form
         v-model:绑定表单中input数据，表单元素input\select。。。对象的属性

         //表单验证
         :rules：表单需要验证，引入验证对象loginRules
         prop="userAccount"：哪一个元素需要验证，引入对象的验证规制userAccount
         绑定数据data
          //验证
      loginRules:{
        userAccount: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      }
      -->
      <el-form  ref="loginForm" :model="loginData" :rules="loginRules">
        <el-form-item prop="userAccount">
          <el-input prefix-icon="el-icon-user" v-model="loginData.userAccount"></el-input>
        </el-form-item>
        <el-form-item prop="userPassword">
          <el-input prefix-icon="el-icon-lock" v-model="loginData.userPassword"></el-input>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
        <el-button type="primary" @click="register">注册</el-button>
      </el-form>
    </div>

  </div>
</template>

<style scoped>
  #login{
    height: 100%;
    background-image: url("../assets/login.jpg");
    background-repeat: no-repeat;
    background-size: 100%;
  }
  #login_div{
    width: 300px;
    position: absolute;
    top: 50%;
    left: 70%;
    transform: translate(-50%,-50%);

  }
</style>
